﻿using System;
using System.Collections;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Http;
using CookComputing.XmlRpc;
using Example01CSharpServer.rpc;

namespace Example01CSharpServer
{
    class Program
    {
        class test : IMyFirstServer
        {

            public int Add(int a, int b)
            {
                Console.WriteLine("{0}.Add({1}, {2}) called", this, a, b);
                return a + b;
            }

            SampleClass bla = null;

            public SampleClass GetSampleInstance()
            {
                Console.WriteLine("{0}.GetSampleInstance() called, bla is {1}", this, bla);
                return bla;
            }

            public void SetSampleInstance(SampleClass input)
            {
                Console.WriteLine("{0}.SetSampleInstance({1})", this, input);
                bla = input;
            }
        }



        static void Main(string[] args)
        {
            MyFirstServerXmlRpcServerDeclaration.Instance = new test();

            IDictionary props = new Hashtable();
            props["name"] = "localhost";
            props["port"] = 1860;
            HttpChannel channel = new HttpChannel(
               props,
               null,
               new XmlRpcServerFormatterSinkProvider()
            );
            
            ChannelServices.RegisterChannel(channel, false);

            RemotingConfiguration.RegisterWellKnownServiceType(
              typeof(MyFirstServerXmlRpcServerDeclaration),
              "rpc",
              WellKnownObjectMode.Singleton);
            Console.WriteLine("Server started, waiting for shutdown");
            Console.ReadLine();
        }
    }
}
